!include LogicLib.nsh
!include MUI.nsh

Name "XRelayer"

OutFile "xrlyinst.exe"

InstallDir $PROGRAMFILES\xrelayer

RequestExecutionLevel admin

ReserveFile "config.ini"
ReserveFile "icon.ico"
	
; Pages

Page components
Page custom ConfigPage ConfigPageLeave ": Configuration"
Page directory
Page instfiles

UninstPage uninstConfirm
UninstPage instfiles

Var ListeningPort

Section "XRelayer main program(required)"
	SectionIn RO
	SetOutPath $INSTDIR
	File "xrelayer.exe"
	File "xrpasswd.exe"
	File "Microsoft.VC80.CRT.manifest"
	File "msvcp80.dll"
	File "msvcr80.dll"
	File "log.conf"
    
	FileOpen $0 $INSTDIR\config.xml w
	FileWrite $0 "<httpproxy><listen>0.0.0.0</listen><port>$ListeningPort</port></httpproxy>"
	FileClose $0

	; Write the uninstall keys for Windows
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\XRelayer" "DisplayName" "XRelayer"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\XRelayer" "UninstallString" '"$INSTDIR\uninstall.exe"'
	WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\XRelayer" "NoModify" 1
	WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\XRelayer" "NoRepair" 1
	WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\XRelayer" "IsService" 0
	WriteUninstaller "uninstall.exe"
SectionEnd

SectionGroup /e "Run XRelayer as a windows service"
	Section
		Var /GLOBAL installcmd
		StrCpy $installcmd '"$INSTDIR\xrelayer.exe" --installservice'
	SectionEnd
	
	Section /o "Automatically start the service when booting"
		StrCpy $installcmd "$installcmd --autostart"
	SectionEnd

	Section
		ExecWait $installcmd
		WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\XRelayer" "IsService" 1
	SectionEnd
	
	Section /o "Start the service after install"
		ExecWait '"$SYSDIR/net.exe" start "XRelayer"'
	SectionEnd
SectionGroupEnd

Function .onInit
	InitPluginsDir
	File /oname=$PLUGINSDIR\icon.ico "icon.ico"
	File /oname=$PLUGINSDIR\config.ini "config.ini"
	WriteINIStr $PLUGINSDIR\config.ini "Field 4" "Text" $PLUGINSDIR\icon.ico
FunctionEnd

Function ConfigPage
	InstallOptions::dialog "$PLUGINSDIR\config.ini"
FunctionEnd

Function ConfigPageLeave
	ReadINIStr $R0 "$PLUGINSDIR\config.ini" "Field 2" State

	${If} $R0 == ""
	    MessageBox MB_OK "Please setup a listening pot"
	    Abort
	${Else}
	    StrCpy $ListeningPort $R0
	${EndIf}
FunctionEnd
;--------------------------------

; Uninstaller

Section "Uninstall"
	ReadRegDWORD $0 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\XRelayer" "IsService"
	IntCmpU $0 1 0 +2 +2
		ExecWait '"$INSTDIR\xrelayer.exe" --removeservice'
  
	; Remove registry keys
	DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\XRelayer"

	; Remove files and uninstaller
	Delete $INSTDIR\config.xml
	Delete $INSTDIR\log.conf
	Delete $INSTDIR\xrelayer.exe
	Delete $INSTDIR\xrpasswd.exe
	Delete $INSTDIR\Microsoft.VC80.CRT.manifest
	Delete $INSTDIR\msvcp80.dll
	Delete $INSTDIR\msvcr80.dll
	Delete $INSTDIR\uninstall.exe

	; Remove directories used
	RMDir "$INSTDIR"

SectionEnd
